package view

import (
	"AccessAdmin/internal/consts"
	"AccessAdmin/internal/model"
	"AccessAdmin/internal/service"
	"github.com/gogf/gf/v2/frame/g"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/gogf/gf/v2/os/gtime"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gmode"
	"github.com/gogf/gf/v2/util/guid"
	"sort"
	"strings"
)

// 视图自定义方法管理对象
type viewBuildIn struct {
	httpRequest *ghttp.Request
}

// StatusMap 返回状态选项Map。
func (s *viewBuildIn) StatusMap() map[int]string {
	statusMap := make(map[int]string)
	statusMap[consts.StatusEnable] = "启用"
	statusMap[consts.StatusDisEnable] = "禁用"
	return statusMap
}

// StatusStr 返回状态字符串
func (s *viewBuildIn) StatusStr(order int, add int) string {
	arrayStatus := []int{consts.StatusDisEnable, consts.StatusEnable}
	if order == 1 {
		sort.Ints(arrayStatus)
	} else {
		sort.Sort(sort.Reverse(sort.IntSlice(arrayStatus)))
	}
	var str string
	for _, status := range arrayStatus {
		if len(str) > 0 {
			str += "," + gconv.String(status+add) + "=" + s.StatusName(status)
		} else {
			str += gconv.String(status+add) + "=" + s.StatusName(status)
		}
	}
	return str
}

// StatusName 根据状态字段内容返回文字。
func (s *viewBuildIn) StatusName(status int) string {
	switch status {
	case consts.StatusEnable:
		return "启用"
	case consts.StatusDisEnable:
		return "禁用"
	default:
		return ""
	}
}

// DropDownList 下拉选择框
// attribute： "name|label|is_must|reqText|isInline"  eg : "status|状态｜1｜请选择状态｜0"
// data： "1=启用,2=禁用"
// value: 1
func (s *viewBuildIn) DropDownList(attribute string, data string, value int) (string, error) {
	var dataArr []*model.DropDownDataItem
	attrArr := strings.Split(attribute, "|")
	arr := strings.Split(data, ",")
	for _, item := range arr {
		temp := strings.Split(item, "=")
		dataArr = append(dataArr, &model.DropDownDataItem{
			Name:  temp[1],
			Value: temp[0],
		})
	}
	ReqText := attrArr[3]
	if ReqText == "" {
		ReqText = "请选择" + attrArr[0]
	}
	viewData := make(g.Map)
	viewData["MainTpl"] = "widgets/dropDownList.html"
	viewData["Data"] = model.DropDownListItem{
		Name:     attrArr[0],
		Value:    value,
		Label:    attrArr[1],
		IsMust:   attrArr[2] == "1",
		ReqText:  ReqText,
		IsInline: attrArr[4] == "1",
		Data:     dataArr,
	}
	return s.httpRequest.Response.ParseTpl(g.Cfg().MustGet(s.httpRequest.Context(), "viewer.noLayout").String(), viewData)
}

// RenderUpload 上传文件小组件
func (s *viewBuildIn) RenderUpload(name string, value string, label string, isMust bool, reqText string, fileType string) (string, error) {
	if label == "" {
		label = "图片"
	}
	if reqText == "" {
		reqText = "请上传图片！"
	}
	if fileType == "" {
		fileType = "图片"
	}
	sId := guid.S()
	viewData := make(g.Map)
	viewData["MainTpl"] = "widgets/upload.html"
	viewData["Data"] = model.UploadItem{
		Name:          name,
		Value:         value,
		Label:         label,
		IsMust:        isMust,
		ReqText:       reqText,
		ButtonContent: "上传" + label,
		ButtonId:      "button_" + sId,
		ImgId:         "img_" + sId,
		UploadUrl:     "/admin/common/upload-file",
		BaseUrl:       "/resource/",
		FileType:      fileType,
	}
	return s.httpRequest.Response.ParseTpl(g.Cfg().MustGet(s.httpRequest.Context(), "viewer.noLayout").String(), viewData)
}

// BackendPrefix 后台路由前缀
func (s *viewBuildIn) BackendPrefix() string {
	return service.Debris().GetBackendPrefix(s.httpRequest.Context())
}

// Version 随机数 开发环境时间戳，线上为前端版本号
func (s *viewBuildIn) Version() string {
	var rand string
	if gmode.IsDevelop() {
		rand = gconv.String(gtime.TimestampMilli())
	} else {
		rand = consts.Version
	}
	return rand
}
